<?php
namespace app\user\controller;

use think\Controller;
use think\Db;
use app\user\model\UserModel;

class Apis extends Controller
{
    public function addUser($user)
    {
        $user['user_ctime'] = date('Y-m-d H:i:s', time());
        $UserModel = new UserModel();
        $res = $UserModel->addUser($user);
        return json($res);
    }

    public function delUserById($id)
    {
        $UserModel = new UserModel();
        $res = $UserModel->delUser($id);
        return json($res);
    }

    public function upUserById($user)
    {
        $user['user_utime'] = date('Y-m-d H:i:s', time());
        $UserModel = new UserModel();
        $res = $UserModel->upUser($user);
        return json($res);
    }

    public function getAllUser()
    {
        $UserModel = new UserModel();
        $res = $UserModel->getUser(null);
        return json($res);
    }

    public function login($user)
    {
        $loginData = array('user_name'=>$user['user_name'], 'user_pwd'=>$user['user_pwd']);
        $UserModel = new UserModel();
        $res = $UserModel->getUser($loginData);
        if (sizeof($res) > 0) {
            session('user_id', $res[0]['user_id']);
            $res = 1;
        } else {
            $res = 0;
        }
        return json($res);
    }

    public function logout(){
        session(null);
        $this->redirect('/index/login');
    }
}
